package mshop.com.schild_kotlin.fragments.search

import android.content.DialogInterface
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import com.orhanobut.logger.Logger
import io.realm.Realm
import io.realm.RealmResults
import kotlinx.android.synthetic.main.fragment_search_history.*
import mshop.com.schild_kotlin.R
import mshop.com.schild_kotlin.activitys.SearchActivity
import mshop.com.schild_kotlin.base.BaseFragment
import mshop.com.schild_kotlin.dialogs.TipsDialog
import mshop.com.schild_kotlin.realm.model.SearchHistoryModel

/**
 * Created by mayi on 2018/5/28.
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO
 */
class HistoryFragment : BaseFragment(), View.OnClickListener {
    override fun getNeedRefresh(): Boolean {
        return false
    }

    override fun onRefresh() {
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.del_history -> {
                var tipsDialog: TipsDialog? = context?.let { TipsDialog(it) }

                tipsDialog?.setTitle("清空记录")
                        ?.setMessage("确定是否删除历史记录？")
                        ?.setPositiveButton("确定", object : DialogInterface.OnClickListener {
                            override fun onClick(p0: DialogInterface?, p1: Int) {
                                realm.executeTransaction {
                                    historyArr?.let {
                                        it.deleteAllFromRealm()
                                        flex_layout.removeAllViews()
                                        no_data_tv.visibility=View.VISIBLE
                                        scroll_layout.visibility=View.GONE
                                    }
                                }
                            }

                        })
                        ?.setNegativeButton("取消", null)
                        ?.toShow()
            }
        }
    }


    val realm: Realm by lazy {
        Realm.getDefaultInstance()
    }


    companion object {
        fun getInstall(): HistoryFragment {
            val fragment = HistoryFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getLayoutResources(): Int {
        return R.layout.fragment_search_history
    }

    var historyArr: RealmResults<SearchHistoryModel>? = null
    override fun initView() {
        historyArr = realm.where(SearchHistoryModel::class.java).findAll()
        historyArr?.let {
            for (history in it) {
                Logger.d("history :" + history.search_key)
                var itemView = layoutInflater.inflate(R.layout.item_search_history, flex_layout, false) as TextView
                itemView.text = history.search_key
                itemView.setOnClickListener(object : View.OnClickListener {
                    override fun onClick(view: View?) {
                        (activity as SearchActivity).let {
                            it.toSearch((view as TextView).text.toString())
                        }
                    }
                })
                flex_layout.addView(itemView)
                del_history.setOnClickListener(this)
            }
            if(it.size>0){
                no_data_tv.visibility=View.GONE
                scroll_layout.visibility=View.VISIBLE
            }
        }

    }

    fun addHistory(searchKey: String) {
        if (TextUtils.isEmpty(searchKey)) {
            return
        }

        //判断是否有重复的搜索关键词
        historyArr?.let {
            for (history in it){
                if (TextUtils.equals(history.search_key,searchKey)){
                    return
                }
            }
        }

        realm.beginTransaction()
        val historyModel = realm.createObject(SearchHistoryModel::class.java)
        historyModel.search_key = searchKey
        realm.commitTransaction()
    }


    override fun onDestroy() {
        super.onDestroy()
        realm.close()
    }
}